import { _decorator, Component, Node, systemEvent, input, SystemEvent, Input, Quat, Vec3, CCFloat, v3, math } from 'cc';
import { EasyController, EasyControllerEvent } from './EasyController';
const { ccclass, property } = _decorator;

const v3_0 = v3();

@ccclass('CameraController')
export class CameraController extends Component {
    @property
    zoomSensitivity: number = 1.0;

    @property
    lenMin: number = 1.0;

    @property
    lenMax: number = 10.0;

    @property
    len: number = 5;

    private _targetLen: number = 0;

    private _targetAngles: Vec3 = v3();

    tweenTime: number = 0.2;

    start() {
        EasyController.on(EasyControllerEvent.CAMERA_ZOOM, this.OnCameraZoom, this);
        this._targetLen = this.len;
    }

    onDestroy() {
        EasyController.off(EasyControllerEvent.CAMERA_ZOOM, this.OnCameraZoom, this);
    }

    SetTargetAngle(x: number, y: number, z: number) {
        this._targetAngles.set(x, y, z);
    }

    OnCameraZoom(delta: number) {
        this._targetLen += delta * this.zoomSensitivity;
        if (this._targetLen < this.lenMin) {
            this._targetLen = this.lenMin;
        }
        if (this._targetLen > this.lenMax) {
            this._targetLen = this.lenMax;
        }
    }

    lateUpdate(deltaTime: number) {
        const t = Math.min(deltaTime / this.tweenTime, 1.0);
        v3_0.set(this.node.eulerAngles);
        Vec3.lerp(v3_0, v3_0, this._targetAngles, t);
        this.node.setRotationFromEuler(v3_0);
    }
}